<?php
declare (strict_types=1);

namespace app\service;

use JetBrains\PhpStorm\Pure;
use MongoDB\Driver\Query;
use think\App;
use app\model\FetchUrlModel;
use app\service\TaskService;


class MarketService extends \think\Service
{
    function fetchTop()
    {
        if ($this->isTopFetched()) {
            return true;
        }
        //采集 从  https://www.maigoo.com/citiao/top_12147.html?page=1 到 https://www.maigoo.com/citiao/top_12147.html?page=20 ;保存到app_path('data/market_top_page')目录
        for ($i = 1; $i <= 20; $i++) {
            $url = "https://www.maigoo.com/citiao/top_12147.html?page=" . $i;
            $html = file_get_contents($url);
            file_put_contents(app_path('data/market_top_page') . "html/page_" . $i . ".html", $html);
        }
        return $this->isTopFetched();
    }

    function isTopFetched()
    {
        for ($i = 1; $i <= 20; $i++) {
            if (!file_exists(app_path('data/market_top_page') . "html/page_" . $i . ".html")) {
                return false;
            }
        }
        return true;
    }

    function parseTopPage()
    {
        // 遍历 app_path('data/market_top_page') . "/html/" 里面所有页面，
        $dataFile = app_path('data') . "data.text";
        if (file_exists($dataFile) && $data = file($dataFile)) {
            return $data;
        }
        for ($i = 1; $i <= 20; $i++) {
            $html = file_get_contents(app_path('data/market_top_page') . "/html/page_" . $i . ".html");
            preg_match_all('/<a\s+(?:[^>]*?\s+)?href="([^"]*)"/i', $html, $matches);
            foreach ($matches[1] as $href) {
                if (is_int(strpos($href, '/top/'))) {
                    file_put_contents($dataFile, "\n" . $href, FILE_APPEND);
                }
            }
        }
        return file($dataFile);
    }

    function getLevel2Urls()
    {
        $rs = $this->parseTopPage();
        return $this->formatUrlsArr($rs);
    }


    static function formatUrlsArr($rs)
    {
        $rs = array_map('trim', $rs);
        $rs = array_filter($rs);
        return array_unique($rs);
    }

    function fetchLevel2Page()
    {
        $urls = self::getLevel2Urls();
        $dir = app_path('data/market_level_2') . "html/";
        $lastFile = $dir . basename(end($urls));
        if (!file_exists($lastFile)) {
            foreach ($urls as $key => $url) {
                $html = file_get_contents($url);
                $file = $dir . basename($url);
                $rt = file_put_contents($file, $html);
            }
        }
        return file_exists($lastFile);
    }

    function parseLevel2Page()
    {
        $urls = self::getLevel2Urls();
        $htmlDir = app_path('data/market_level_2') . "html/";
        $fileUrl = app_path('data/market_level_2') . 'urls';
        $isParsed = true;
        if (file_exists($fileUrl)) {
            $tmpCount = count(file($fileUrl));
            $isParsed = $tmpCount > 20;
        }
        $rsAll = [];
        $isParsed = false;
        if (!$isParsed) {
            foreach ($urls as $key => $url) {
                $baseName = basename($url);
                $tmp = explode('.', $baseName);
                $a = $tmp[0];
                $contents = file_get_contents($htmlDir . $baseName);
                $len = strlen($contents);
                $pattern = '/<a\s+[^>]*?href="([^"]*)"[^>]*?>(.*?)<\/a>/i';
                preg_match_all($pattern, $contents, $matches);
                //$hrefs = isset($matches['href']) ? $matches['href'] : [];
                foreach ($matches[1] as $index => $href) {
                    $text=$matches[2][$index];
                    if (!strpos($href, '/citiao/')) {
                        continue;
                    }
                    if(!self::hasKeyword($text)) {
                         continue;
                    }
                    $rsAll []=$href;

                }
            }
        }
        $rsAll = array_unique($rsAll);
        $rs = self::formatUrlsArr($rsAll);
        return $rs;
    }

    static function hasKeyword($text)
    {
        $keySet = ['果', '农', '菜'];
        foreach ($keySet as $key) {
            if (strpos($text, $key)) {
                return true;
            }
        }
        return false;
    }

    function getUrlContents()
    {
        $urlSet = $this->parseLevel2Page();
        $htmlDir = app_path('data/market_content') . "html/";
        $rs = glob($htmlDir . '/*.html');
        $rs = [];
        if (!$rs) {
            foreach ($urlSet as $url) {
                $url = self::formatUrl($url);
                $contents = file_get_contents($url);
                $len = strlen($contents);

                $file = $htmlDir . trim(basename($url));

                $rt = file_put_contents($file, $contents);
                echo "\n$len $rt $file";
            }
        }
        return glob($htmlDir . '/*.html');
    }

    static function formatUrl($url)
    {
        if (strpos($url, '#')) {
            $url = strstr($url, '#', true);
        }
        return $url;
    }


    function parseTop()
    {
        //app_path('data/market_top_page')目录 /html/body/div[1]/div/div[1]/div/div[1]/div[2]/div[16]/a
    }
}
